package nz.broto.animatedlabel;

import java.awt.Color;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.event.MouseInputListener;

import org.jdesktop.swingx.JXLabel;

@SuppressWarnings("serial")
public class JXLabelMove extends JXLabel implements MouseInputListener{

	private Color baseColor = Color.BLACK;
	private Color pressedColor = Color.WHITE;
	private Color passedColor = Color.GRAY;
	private ImageIcon baseIcon = null;
	private ImageIcon pressedIcon = null;
	private ImageIcon passedIcon = null;
	private JXLabelMoveGroup group = null;
	
	private int mode;
	private int state;
	
	public static final int BASE_STATE = 0;
	public static final int PRESSED_STATE = 1;
	public static final int PASSED_STATE = 2;
	
	public static final int JUST_LABEL = 0;
	public static final int JUST_ICON = 1;
	public static final int BOTH_ICON_LABEL = 2;
	
	public JXLabelMove(){
		addMouseListener(this);
		addMouseMotionListener(this);
		
		mode = JUST_LABEL;
	}
	
	public JXLabelMove(Color baseColor, Color pressedColor, Color passedColor){
		this.baseColor = baseColor;
		this.pressedColor = pressedColor;
		this.passedColor = passedColor;
		
		mode = JUST_LABEL;
		
		setState(BASE_STATE);
		
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	
	public JXLabelMove(ImageIcon baseIcon, ImageIcon pressedIcon, ImageIcon passedIcon){
		this.baseIcon = baseIcon;
		this.pressedIcon = pressedIcon;
		this.passedIcon = passedIcon;
		
		mode = JUST_ICON;
		
		setState(BASE_STATE);
		
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	
	public JXLabelMove(Color baseColor, Color pressedColor, Color passedColor, ImageIcon baseIcon, ImageIcon pressedIcon, ImageIcon passedIcon){
		this.baseColor = baseColor;
		this.pressedColor = pressedColor;
		this.passedColor = passedColor;
		
		this.baseIcon = baseIcon;
		this.pressedIcon = pressedIcon;
		this.passedIcon = passedIcon;
		
		mode = BOTH_ICON_LABEL;
		
		setState(BASE_STATE);
		
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	private void setBaseState(){
		switch (mode) {
		case JUST_LABEL:
			this.setForeground(baseColor);
			break;
			
		case JUST_ICON:
			this.setIcon(baseIcon);
			break;
			
		case BOTH_ICON_LABEL:
			this.setForeground(baseColor);
			this.setIcon(baseIcon);
			break;
			
		default:
			break;
		}
		
	}
	
	private void setPressedState(){
		switch (mode) {
		case JUST_LABEL:
			this.setForeground(pressedColor);
			break;
			
		case JUST_ICON:
			this.setIcon(pressedIcon);
			break;
			
		case BOTH_ICON_LABEL:
			this.setForeground(pressedColor);
			this.setIcon(pressedIcon);
			break;
			
		default:
			break;
		}
		
		if (group != null){
			group.mousePassed(this);
		}
	}
	
	private void setPassedState(){
		switch (mode) {
		case JUST_LABEL:
			this.setForeground(passedColor);
			break;
			
		case JUST_ICON:
			this.setIcon(passedIcon);
			break;
			
		case BOTH_ICON_LABEL:
			this.setForeground(passedColor);
			this.setIcon(passedIcon);
			break;
			
		default:
			break;
		}
		
		if (group != null){
			group.mousePassed(this);
		}
	}
	
	public int getState() {
		return state;
	}

	public void setState(int state) {
		switch (state) {
		case BASE_STATE:
			setBaseState();
			break;

		case PRESSED_STATE:
			setPressedState();
			break;
			
		case PASSED_STATE:
			setPassedState();
			break;
			
		default:
			break;
		}
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		if (mode == JUST_ICON || mode == BOTH_ICON_LABEL){
			if (baseIcon == null || pressedIcon == null || passedIcon == null){
				mode = JUST_LABEL;
				throw new NullPointerException();
			}else{
				this.mode = mode;
			}
		}else{
			this.mode = mode;
		}
	}
	
	public Color getBaseColor() {
		return baseColor;
	}

	public void setBaseColor(Color baseColor) {
		this.baseColor = baseColor;
	}

	public Color getPressedColor() {
		return pressedColor;
	}

	public void setPressedColor(Color pressedColor) {
		this.pressedColor = pressedColor;
	}

	public Color getPassedColor() {
		return passedColor;
	}

	public void setPassedColor(Color passedColor) {
		this.passedColor = passedColor;
	}
	
	public ImageIcon getBaseIcon() {
		return baseIcon;
	}

	public void setBaseIcon(ImageIcon baseIcon) {
		this.baseIcon = baseIcon;
	}

	public ImageIcon getPressedIcon() {
		return pressedIcon;
	}

	public void setPressedIcon(ImageIcon pressedIcon) {
		this.pressedIcon = pressedIcon;
	}

	public ImageIcon getPassedIcon() {
		return passedIcon;
	}

	public void setPassedIcon(ImageIcon passedIcon) {
		this.passedIcon = passedIcon;
	}

	public JXLabelMoveGroup getGroup() {
		return group;
	}

	public void setGroup(JXLabelMoveGroup group) {
		this.group = group;
	}
	
	public void mousePressed(MouseEvent arg0) {
		setPressedState();
	}

	public void mouseReleased(MouseEvent arg0) {
		setBaseState();
	}

	public void mouseMoved(MouseEvent arg0) {
		setPassedState();
	}
	
	public void mouseClicked(MouseEvent arg0) {}

	public void mouseEntered(MouseEvent arg0) {}

	public void mouseExited(MouseEvent arg0) {}
	
	public void mouseDragged(MouseEvent arg0) {}

}
